from collections import deque
# 四个方向：上、右、下、左
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def bfs(s1, s2):
    q = deque()
    q.append((s1, 0))
    st = set()
    st.add(s1)

    while q:
        now = q.popleft()
        ss, dist = now  # ss:当前状态字符串，dist:当前步数

        if ss == s2:
            return dist

        k = ss.find('.')
        x, y = k // 3, k % 3  # 转换为二维坐标

        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]

            if nx < 0 or ny < 0 or nx > 2 or ny > 2:
                continue

            tmp = list(ss)
            # nx*3+ny：将新坐标转回一维索引
            tmp[k], tmp[nx * 3 + ny] = tmp[nx * 3 + ny], tmp[k]
            tmp = ''.join(tmp)

            if tmp not in st:
                st.add(tmp)
                q.append((tmp, dist + 1))
    return -1

s1 = input()
s2 = input()
print(bfs(s1, s2))
